package com.security.browser.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import static org.springframework.security.core.authority.AuthorityUtils.commaSeparatedStringToAuthorityList;


/**
 * @author td
 * @date 2017/10/20
 */
@Component
public class SecurityUserDetailService implements UserDetailsService {

    private Logger logger = LoggerFactory.getLogger(SecurityUserDetailService.class);

    @Autowired
    private PasswordEncoder passwordEncoder;
    /**
     * 根据用户名查找用户信息,查找到用户信息后返回userDetails，然后spring security框架根据查找到的用户信息比对用户输入的密码是否正确
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("根据用户名进行查找用户信息");
        String password = passwordEncoder.encode("123456");
        logger.info("用户数据库中的密码:"+password);
        // 可以通过dao进行查找
        //根据查找的用户信息，判断用户是否被冻结

       // return new User(username,"123456", AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));


        return new User(username,password,true,true,true,true, commaSeparatedStringToAuthorityList("admin"));

    }
}
